import {mapMutations} from 'vuex'
  import {login}from '../api/login.js'
  
export default {
  methods:{
    ...mapMutations('m_user',['updateUserInfo','updatToken']),
      beforeLogin() {
        console.log('登录中');
              uni.getUserProfile({
                desc: '请求授权',
                success: (res) => {
                  // 将信息存到 vuex 中
                  // this.updateUserInfo(res.userInfo)
                  // this.getToken(res)
                  console.log('授权成功');
                  console.log(res);
                  console.log(res.userInfo);
                  this.updateUserInfo(res.userInfo)
                  this.getToken(res)
                },
          fail: (res) => {
            console.log('授权失败');
            return uni.$showMsg('您取消了登录授权')
          }
              })
    },
    async getToken(info){
      const [err, res] = await uni.login().catch(err=>err)
      console.log(err);
      console.log(res);
      console.log(res.code);
      // if(err || res.Msg !== 'login:ok')
      // return uni.$showError('登录失败')
      
      const query = {
        code: res.code,
        encryptedData: info.encryptedData,
            iv: info.iv,
            rawData: info.rawData,
            signature: info.signature
      }
      console.log(query);
      
      const {data: loginResult} = await login(query)
      console.log(loginResult.meta.status);
      console.log(loginResult.message.token);
      if(loginResult.meta.status === 200) {
        console.log('登录成功了');
      }else{
        console.log('登录失败了');
      }
    }
      
  }
}